{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openpyxl\n",
    "import json\n",
    "\n",
    "excel_file = '001_外管平台_V1.4.xlsx'\n",
    "excel_data = openpyxl.load_workbook(excel_file, read_only=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"安全基线\": {\n",
      "        \"系统\": {\n",
      "            \"umask\": \"022\",\n",
      "            \"启动级别\": \"multi-user.target/3\",\n",
      "            \"rsyslog/syslog\": \"authpriv.*    /var/log/secure\",\n",
      "            \"文件/目录权限\": \"禁止0777及o-w\"\n",
      "        },\n",
      "        \"ssh\": {\n",
      "            \"SSH安全登录\": [\n",
      "                \"PermitRootLogin yes\",\n",
      "                \"IgnoreRhosts yes\",\n",
      "                \"PermitEmptyPasswords no\",\n",
      "                \"HashKnownHosts yes\"\n",
      "            ],\n",
      "            \"Port\": 50022\n",
      "        },\n",
      "        \"History\": {\n",
      "            \"HISTSIZE\": 5000,\n",
      "            \"HISTTIMEFORMAT\": \"%F %H:%M:%S\"\n",
      "        },\n",
      "        \"password\": {\n",
      "            \"password\": \"password required pam_cracklib.so retry=6 minlen=8 lcredit=-2 ocredit=-1\\npassword  sufficient  pam_unix.so md5 shadow nullok try_first_pass use_authtok  remember=5\"\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "title = ('类别', '项目', '参数名', '参数值')\n",
    "excel_data_range = excel_data['Linux-系统参数']['B37:E48']\n",
    "excel_data_dict = {}\n",
    "# 构建内容树\n",
    "for record in excel_data_range:\n",
    "    _type, _project, _param_name, _param_value = map(lambda x: x.value, record)\n",
    "    excel_data_dict.setdefault(_type, {}).setdefault(\n",
    "        _project, {}) .setdefault(_param_name, []).append(_param_value)\n",
    "# 规范最后一层数据格式，如仅有一项则去掉列表，具体情形由实际业务而定\n",
    "for v1 in excel_data_dict.values():\n",
    "    for k2, v2 in v1.items():\n",
    "        for k3 in v2.keys():\n",
    "            if len(v2[k3]) == 1:\n",
    "                v2[k3] = v2[k3][0]\n",
    "\n",
    "print(json.dumps(excel_data_dict, ensure_ascii=False, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "    \"安全基线\": {\n",
      "        \"系统\": {\n",
      "            \"umask\": \"002\",\n",
      "            \"启动级别\": \"multi-user.target/3\",\n",
      "            \"rsyslog/syslog\": \"authpriv.*    /var/log/secure\"\n",
      "        },\n",
      "        \"ssh\": {\n",
      "            \"SSH安全登录\": [\n",
      "                \"PermitRootLogin yes\",\n",
      "                \"IgnoreRhosts yes\",\n",
      "                \"PermitEmptyPasswords no\",\n",
      "                \"HashKnownHosts yes\"\n",
      "            ],\n",
      "            \"Port\": 22\n",
      "        },\n",
      "        \"History\": {\n",
      "            \"HISTSIZE\": 5000,\n",
      "            \"HISTTIMEFORMAT\": \"%F %H:%M\"\n",
      "        }\n",
      "    }\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "import yaml\n",
    "\n",
    "with open(\"生产基线测试.yml\", encoding='utf-8') as yaml_file:\n",
    "    yaml_data_dict = yaml.safe_load(yaml_file)\n",
    "\n",
    "print(json.dumps(yaml_data_dict, ensure_ascii=False, indent=4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 [安全基线]: Pair password:{'password': 'password required pam_cracklib.so retry=6 minlen=8 lcredit=-2  ... is in LHS only\n",
      "2 [安全基线][History][HISTTIMEFORMAT]: %F %H:%M:%S != %F %H:%M\n",
      "3 [安全基线][ssh][Port]: 50022 != 22 (abs: -5.0e+04, rel: -1.0e+00)\n",
      "4 [安全基线][系统]: Pair 文件/目录权限:禁止0777及o-w is in LHS only\n",
      "5 [安全基线][系统][umask]: 022 != 002\n"
     ]
    }
   ],
   "source": [
    "from recursive_diff import recursive_diff\n",
    "\n",
    "for i, diff in enumerate(recursive_diff(excel_data_dict, yaml_data_dict), 1):\n",
    "    print(i, diff)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 附\n",
    "\n",
    "<br>\n",
    "\n",
    "- Excel参数表截图：[Excel参数表截图](./Excel参数表截图.png)\n",
    "- 生产基线测试.yml：[生产基线测试.yml](./生产基线测试.yml)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.7.4 64-bit ('py37': conda)",
   "language": "python",
   "name": "python37464bitpy37conda124ef028e81f4ec7808674584596f802"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
